package com.xxx.blog.service.impl;

import cn.hutool.core.util.RandomUtil;
import com.github.pagehelper.PageHelper;
import com.xxx.blog.domain.Article;
import com.xxx.blog.mapper.ArticleMapper;
import com.xxx.blog.query.ArticleQuery;
import com.xxx.blog.service.IArticleService;
import com.xxx.blog.vo.GdVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
@RequiredArgsConstructor
public class ArticleServiceImpl implements IArticleService {

    private final ArticleMapper articleMapper;

    @Override
    public List<Article> findAll() {
        return articleMapper.findAll();
    }

    @Override
    public Article findOne(Long id) {
        return articleMapper.findOne(id);
    }

    @Override
    public List<Article> queryPage(ArticleQuery ArticleQuery) {
        PageHelper.startPage(ArticleQuery.getCurrentPage(), ArticleQuery.getPageSize());

        return articleMapper.queryPage(ArticleQuery);//limit a,b
    }

    @Override
    public void addArticle(Article Article) {
        String salt = RandomUtil.randomString(32);
        //Article.setSalt(salt);
        //Article.setPassword(SecureUtil.md5(Article.getPassword() + salt));
        articleMapper.addArticle(Article);
    }

    @Override
    public void updateArticle(Article Article) {
        Article.setUpdateTime(new Date());
        articleMapper.updateArticle(Article);
    }

    @Override
    public void deleteArticle(Long id) {
        articleMapper.deleteArticle(id);
    }

    @Override
    public void batchArticles(Long[] ids) {
        articleMapper.batchArticle(ids);
    }

    @Override
    public Article getByFileName(String fileName) {
        return articleMapper.getByFileName(fileName);
    }

    @Override
    public List<GdVo> getGd() {
        return articleMapper.getGd();
    }

    @Override
    public List<Article> getRa(Long id) {
        Article article = articleMapper.findOne(id);

        return articleMapper.getRa(article);
    }

    @Override
    public List<Article> archiveDetail(String yearMonth) {
        return articleMapper.findArticleByYearMonth(yearMonth);
    }
}
